﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Skewworks.Win.Motif.Glide
{
    public partial class GlideSlider : UserControl, IEmbeddedControl
    {

        #region Variables

        ISettings _settings;
        GlideOrientations _dir;
        int _snap;
        int _tick;
        Color _tickColor;
        int _knob;
        int _min;
        int _max;
        int _value;

        #endregion

        public GlideSlider()
        {
            InitializeComponent();
            _dir = GlideOrientations.Horizontal;
            _snap = 10;
            _tick = 10;
            _tickColor = Color.Black;
            _knob = 10;
            _max = 100;
            _value = 0;
            Width = 200;
            Height = 30;
            _settings = new GlideSliderSettings(this);
        }

        #region Properties

        public GlideOrientations Direction
        {
            get { return _dir; }
            set { _dir = value; Invalidate(); }
        }

        public int SnapInterval
        {
            get { return _snap; }
            set { _snap = value; Invalidate(); }
        }

        public int TickInterval
        {
            get { return _tick; }
            set { _tick = value; Invalidate(); }
        }

        public Color TickColor
        {
            get { return _tickColor; }
            set { _tickColor = value; Invalidate(); }
        }

        public int KnobSize
        {
            get { return _knob; }
            set { _knob = value; Invalidate(); }
        }

        public int Value
        {
            get { return _value; }
            set
            {
                if (value > _max)
                    value = _max;
                if (value < _min)
                    value = _min;
                _value = value;
                Invalidate();
            }
        }

        public int Maximum
        {
            get { return _max; }
            set
            {
                if (value > 0)
                    _max = value;

                if (_min >= _max)
                    _max = _min + 1;
                if (_value > _max)
                    _value = _max;

                Invalidate();
            }
        }

        public int Minimum
        {
            get { return _min; }
            set
            {
                if (value > 0)
                    _min = value;

                if (_min >= _max)
                    _min = _max - 1;
                if (_value < _min)
                    _value = _min;

                Invalidate();
            }
        }

        public ISettings Settings
        {
            get { return _settings; }
        }

        #endregion

        #region GUI


        protected override void OnPaint(PaintEventArgs e)
        {
            lock (this)
            {
                e.Graphics.DrawRectangle(Pens.Red, new Rectangle(0, 0, Width - 1, Height - 1));
                StringFormat sf = new StringFormat();
                sf.LineAlignment = StringAlignment.Center;
                sf.Alignment = StringAlignment.Center;
                e.Graphics.DrawString("Pending GHI Response", this.Font, Brushes.Red, new Rectangle(0, 0, Width, Height), sf);
                if (Focused)
                {
                    e.Graphics.DrawRectangle(Pens.Orange, new Rectangle(0, 0, Width - 1, Height - 1));
                    e.Graphics.DrawRectangle(Pens.Orange, new Rectangle(1, 1, Width - 3, Height - 3));
                }
            }
        }

        #endregion

        #region Moving/Sizing

        private bool _sizeV, _sizeH, _move;
        private Point _sizeRef;

        private void GlideSlider_MouseMove(object sender, MouseEventArgs e)
        {
            if (_sizeH || _sizeV)
            {
                if (_sizeH)
                    this.Width += e.X - _sizeRef.X;
                if (_sizeV)
                    this.Height += e.Y - _sizeRef.Y;
                Invalidate();
            }
            else if (_move)
            {
                this.Left += e.X - _sizeRef.X;
                this.Top += e.Y - _sizeRef.Y;
                return;
            }
            else
            {
                if (e.X >= this.Width - 8)
                {
                    if (e.Y >= this.Height - 8)
                        this.Cursor = Cursors.SizeNWSE;
                    else
                        this.Cursor = Cursors.SizeWE;
                }
                else if (e.Y >= this.Height - 8)
                    this.Cursor = Cursors.SizeNS;
                else
                    this.Cursor = Cursors.Default;
            }
            _sizeRef = new Point(e.X, e.Y);
        }

        private void GlideSlider_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.X >= this.Width - 8)
            {
                if (e.Y >= this.Height - 8)
                {
                    _sizeH = true;
                    _sizeV = true;
                }
                else
                    _sizeH = true;
            }
            else if (e.Y >= this.Height - 8)
                _sizeV = true;
            else
            {
                _move = true;
                this.Cursor = Cursors.SizeAll;
            }
        }

        private void GlideSlider_MouseUp(object sender, MouseEventArgs e)
        {
            _sizeH = false;
            _sizeV = false;
            _move = false;
            this.Cursor = Cursors.Default;
        }

        #endregion

    }
}
